package com.sanpu.kxe.common.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.module.roundview.RoundTextView;

import butterknife.ButterKnife;

/**
 * Description：自定义item
 * Created by 薛勇军 on 2018/7/10.
 */

public class PrimaryItem extends LinearLayout {
    protected TextView vTitle;
    protected TextView vDes;
    protected ImageView vIcon;
    protected ImageView vIconRight;
    protected RoundTextView vBadge;
    protected ImageView vArrow;

    protected String title;
    protected String des;
    protected int icon;
    protected int iconRight;
    protected boolean badge;
    protected boolean showArrow;

    public PrimaryItem title(String title) {
        this.title = title;
        return this;
    }

    public PrimaryItem icon(int icon) {
        this.icon = icon;
        return this;
    }

    public PrimaryItem badge(boolean badge) {
        this.badge = badge;
        return this;
    }

    public PrimaryItem des(String des) {
        this.des = des;
        return this;
    }

    public PrimaryItem des(boolean showArrow) {
        this.showArrow = showArrow;
        return this;
    }

    public PrimaryItem(Context context, int id, String title, String des, int icon, int iconRight, boolean badge, boolean showArrow) {
        super(context);
        setId(id);
        this.title = title;
        this.icon = icon;
        this.iconRight = iconRight;
        this.des = des;
        this.badge = badge;
        this.showArrow = showArrow;

        LayoutInflater.from(getContext()).inflate(R.layout.item_primary, this);
        vIcon = ButterKnife.findById(this, R.id.icon);
        vTitle = ButterKnife.findById(this, R.id.title);
        vDes = ButterKnife.findById(this, R.id.des);
        vIconRight = ButterKnife.findById(this, R.id.iconRight);
        vBadge = ButterKnife.findById(this, R.id.badge);
        vArrow = ButterKnife.findById(this, R.id.arrow);
        show();
    }

    public PrimaryItem(Context context, int id, String title, String des, int icon, boolean badge, boolean showArrow) {
        this(context, id, title, des, icon, 0, badge, showArrow);
    }

    public PrimaryItem(Context context, int id, String title, int icon, boolean badge, boolean showArrow) {
        this(context, id, title, "", icon, 0, badge, showArrow);
    }

    public PrimaryItem(Context context, int id, String title, int icon, int iconRight, boolean badge, boolean showArrow) {
        this(context, id, title, "", icon, iconRight, badge, showArrow);
    }

    public PrimaryItem(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
    }

    public PrimaryItem show() {
        vTitle.setText(title);
        vDes.setText(des);
        if (icon > 0)
            vIcon.setBackgroundResource(icon);
        vBadge.setVisibility(badge ? View.VISIBLE : View.GONE);
        vArrow.setVisibility(showArrow ? VISIBLE : INVISIBLE);
        if (iconRight > 0) {
            vIconRight.setVisibility(VISIBLE);
            vIconRight.setImageResource(iconRight);
        } else {
            vIconRight.setVisibility(GONE);
        }
        return this;
    }

    public void setDes(String des, int color) {
        if (!TextUtils.isEmpty(des)) {
            vDes.setText(des);
            vDes.setTextColor(color);
        }
    }

    public void setDes(String des) {
        if (!TextUtils.isEmpty(des)) {
            vDes.setText(des);
        }
    }
}
